Imports AppliedRecognition.Util
Imports AppliedRecognition.Speech.TaskSupport
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class ResizePreviewForm

    Inherits QSBaseForm

    Private TS As New ARTraceSwitch("Resize", "Resize Commands")
    Private _WinSizeOptions As Hashtable
    Private _BaseWindow As WindowItem

#Region " Windows Form Designer generated code "


    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'ResizePreviewForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Fuchsia
        Me.ClientSize = New System.Drawing.Size(848, 744)
        Me.Name = "ResizePreviewForm"
        Me.Opacity = 0.5
        Me.ShowInTaskbar = False
        Me.Text = ""
        Me.TransparencyKey = System.Drawing.Color.Fuchsia

    End Sub

#End Region

#Region "Singlton"

    Private Shared _instance As ResizePreviewForm = Nothing
    Private Shared _lock As Object = New Object

    '
    ' this Singleton is only used to ensure a single instance while the form is visible.
    ' that is to say it's not a memory management feature
    Public Shared ReadOnly Property Instance() As ResizePreviewForm
        Get
            If _instance Is Nothing Then
                SyncLock _lock
                    If _instance Is Nothing Then
                        _instance = New ResizePreviewForm
                        _instance.TS.WVerbose("New Instance")
                    End If
                End SyncLock
            End If
            Return _instance
        End Get
    End Property
    Private Sub Form_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        _instance = Nothing
        TS.WVerbose("Closed")
    End Sub
    Public Shared ReadOnly Property Exists() As Boolean
        Get
            If _instance Is Nothing Then Return False
            Return True
        End Get
    End Property
#End Region

    Private Sub New()
        MyBase.New()


        InitializeComponent()
        TS.Level = My.MySettings.Default.QSTraceLevel
        HideFormTimer.Interval = My.MySettings.Default.DisplayTimout
    End Sub

    Protected Overrides Function ShowFormSetup() As Boolean
        Dim curRect As Rectangle

        Dim cur As Size

        Dim idx As Integer = 0
        Dim useLetters As Boolean = False

        If BaseWindow Is Nothing Then
            'Me.Dispatch.ShowUserError("No active window exists.")
            Exit Function
        End If

        _WinSizeOptions = New Hashtable(60)

        Dim baseRect As Rectangle = BaseWindow.Rect

        Dim ps As Screen = Windows.Forms.Screen.PrimaryScreen
        Dim screenBounds As Rectangle = ps.WorkingArea

        Dim screenStep As Size

        screenStep.Width = CInt(0.1D * screenBounds.Width)
        screenStep.Height = CInt(0.1D * screenBounds.Height)



        Trace.Indent()

        TS.WVerbose(String.Format("{0}	{1}	{2}", "Step", "Height", "Width"))    '	{3}	{4}
        Dim increment As Size
        For ff As Integer = 1 To 9

            increment.Width = screenStep.Width * ff
            increment.Height = screenStep.Height * ff

            cur.Width = baseRect.Width - increment.Width
            cur.Height = baseRect.Height - increment.Height

            If cur.Width > screenStep.Width AndAlso cur.Height > screenStep.Height Then
                curRect = New Rectangle(baseRect.Location, cur)
                TS.WVerbose(String.Format("{0}	{1}	{2}", ff, curRect.Size.Height, curRect.Width))     '	{3}	{4}
                _WinSizeOptions.Add((-ff).ToString, curRect)
            End If

        Next
        Trace.Unindent()

        '		ResizeTS.WVerbose("ScreenRect" + screenBounds.ToString & "ScreenBounds=" & screenBounds.Right & " " & screenBounds.Bottom)

        For ff As Integer = 1 To 100
            increment.Width = screenStep.Width * ff
            increment.Height = screenStep.Height * ff

            cur.Width = baseRect.Width + increment.Width
            cur.Height = baseRect.Height + increment.Height
            curRect = New Rectangle(baseRect.Location, cur)


            If curRect.Right > screenBounds.Right Then
                curRect.Width = screenBounds.Width - curRect.X
            End If

            If curRect.Bottom >= screenBounds.Bottom Then
                curRect.Height = screenBounds.Height - curRect.Y
            End If


            'If CInt(ff) > 4 Then Debug.WriteLine(ff & " " & curRect.ToString & " " & curRect.Right & " " & curRect.Bottom)


            If curRect.Bottom >= screenBounds.Bottom AndAlso curRect.Right >= screenBounds.Right Then
                TS.WVerbose("exiting" & _WinSizeOptions.Count)
                Exit For
            End If

            _WinSizeOptions.Add(ff.ToString, curRect)

        Next
        Return True
    End Function

    Private Sub doDraw(ByVal g As Graphics)
        Dim labelLocation As PointF
        Dim text As String
        Dim lineColor As Color
        Dim linePen As Pen


        lineColor = Color.FromArgb(CByte(0.2 * 255), Color.Blue)

        linePen = New Pen(lineColor, 4)
        linePen.EndCap = Drawing2D.LineCap.Round

        For Each de As DictionaryEntry In _WinSizeOptions
            Dim r As Rectangle = DirectCast(_WinSizeOptions(de.Key), Rectangle)
            g.DrawRectangle(linePen, r)
            text = de.Key.ToString

            Dim sf As SizeF = g.MeasureString(text, Dispatch.Cfg.NumFont)
            labelLocation.X = r.Right - sf.Width
            labelLocation.Y = r.Bottom - sf.Height


            g.FillRectangle(Brushes.Yellow, New RectangleF(labelLocation, sf))
            g.DrawString(text, Dispatch.Cfg.NumFont, Brushes.Black, labelLocation)

        Next
        linePen.Dispose()

    End Sub


    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        If _WinSizeOptions Is Nothing Then Exit Sub
        doDraw(e.Graphics)
    End Sub


    Public Sub ResizeWindow(ByVal sizeOption As String)
        Try
            Dim key As String = sizeOption
            If _WinSizeOptions Is Nothing Then
                '				Dispatch.ShowUserError("No size options are active")
                Exit Sub
            End If

            If Not _WinSizeOptions.Contains(key) Then
                TS.WWarning("Unable to find size " + key)
                '				Dispatch.ShowUserError("Resize option " + key + "can't be found")
                Exit Sub
            End If

            Dim r As Rectangle = DirectCast(_WinSizeOptions(key), Rectangle)
            BaseWindow.Resize(r.Size)

        Finally
            '            ResetResize()
            '           BaseWindow.Restore()
        End Try
    End Sub

    Private ReadOnly Property BaseWindow() As WindowItem
        Get
            If _BaseWindow Is Nothing Then
                _BaseWindow = Dispatch.WindowTasks.ActiveWindow
            End If
            Return _BaseWindow
        End Get
    End Property


    'Friend Sub ResetResize()
    '    HideFormTimer.Enabled = False
    '    _WinSizeOptions = Nothing
    '    _BaseWindow = Nothing
    '    Me.Hide()
    'End Sub


End Class


Friend Class WinPlacementLabel
    Public DisplayLabel As String
    Public SpokenLabel As String
End Class

Friend Class WinPlacementLabelGen

    Public Function NextLabel() As WinPlacementLabel
        Throw New NotImplementedException
    End Function

    Public Sub AssignLabelToRect(ByVal label As WinPlacementLabel, ByVal rect As Rectangle)

    End Sub

End Class



